/*
 *  Copyright 2011-2012 Redia A/S
 * 
 *  This file is part of Redias Android Utilities.
 *
 *  Redias Android Utilities is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Redias Android Utilities is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Redias Android Utilities.  If not, see <http://www.gnu.org/licenses/>.
 */
package dk.redia.lib.model;

import java.net.MalformedURLException;
import java.net.URL;

import android.os.Parcel;
import android.os.Parcelable;

public class Media implements Parcelable, Comparable<Media> {
	
	public static final int TYPE_UNKNOWN = 0;
	public static final int TYPE_IMAGE = 2;
	public static final int TYPE_VIDEO = 4;
	public static final int TYPE_AUDIO = 6;
	
	private AbsoluteId id;
	private URL url;
	private String title;
	private String inThePicture;
	private int type;
	private URL thumbUrl;
	private String description;
	private String artist;
	private String year;
	
	public String getTitle() {
		return title;
	}
	
	public String getArtist() {
		return this.artist;
	}
	
	public void setArtist(String artist) {
		this.artist = artist;
	}

	public void setTitle(String title) {
		this.title = title;
	}
	
	public Media(int type) {
		this.type = type;
	}
	
	public Media(int type, String url) throws MalformedURLException {
		this.url = new URL(url);
		this.type = type;
	}
	
	public Media(int type, URL url) {
		this.url = url;
		this.type = type;
	}
	
	public URL getUrl() {
		return this.url;
	}
	
	public int getType() {
		return this.type;
	}

	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	public void writeToParcel(Parcel dest, int arg1) {
//		if (url==null) {
//			arg0.writeString("");
//		} else {
//			arg0.writeString(url.toExternalForm());
//		}
		URL url = getUrl();
		dest.writeInt(type);
		dest.writeString(url.getHost());
		//dest.writeString(url.getPath());
		dest.writeInt(url.getPort());
		//dest.writeString(url.getAuthority());
		dest.writeString(url.getFile());
		dest.writeString(url.getProtocol());
		//dest.writeString(url.getQuery());
	}
	
	public static final Parcelable.Creator<Media> CREATOR = new Parcelable.Creator<Media>() {

		public Media createFromParcel(Parcel source) {
//			String url = arg0.readString();
			int type = source.readInt();
//			if (url.equals("")) {
//				throw new RuntimeException("Invalid URL read during Media deserialization");
//			}
//			try {
				try {
					String host = source.readString();
					//String path = source.readString();
					int port = source.readInt();
					//String auth = source.readString();
					String file = source.readString();
					String protocol = source.readString();
					//String query = source.readString();
					URL url = new URL(protocol, host, port, file);
					
					return new Media(type, url);
				} catch (MalformedURLException e) {
					e.printStackTrace();
				}
				return null;
//			} catch (MalformedURLException e) {
//				e.printStackTrace();
//				throw new RuntimeException(e);
//			}
		}

		public Media[] newArray(int arg0) {
			return new Media[arg0];
		}
	};

	public int compareTo(Media arg0) {
		if (this.title==null && arg0.title!=null) {
			return -1;
		} else if (this.title!=null && arg0.title==null) {
			return 1;
		} else if (this.title==null && arg0.title==null) {
			return 0;
		} else {
			return this.title.compareTo(arg0.title);
		}
	}

	public String getInThePicture() {
		return inThePicture;
	}

	public void setInThePicture(String inThePicture) {
		this.inThePicture = inThePicture;
	}

	public void setUrl(String text) throws MalformedURLException {
		url = new URL(text);
	}
	
	public void setId(AbsoluteId id) {
		this.id = id;
	}
	
	public AbsoluteId getId() {
		return id;
	}

	public void setThumbUrl(String url) throws MalformedURLException {
		this.thumbUrl = new URL(url);
	}
	
	public URL getThumbUrl() {
		return this.thumbUrl;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	
	public String getDescription() {
		return this.description;
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}
}
